//
//  XueXiController.m
//  GuShiNews
//
//  Created by 丁一 on 2019/8/23.
//  Copyright © 2019 Jones. All rights reserved.
//

#import "XueXiController.h"
#import "HomeNewsCell.h"
#import "NewsAction.h"
#import "NewsModel.h"
#import "SideNewsViewModel.h"
#import "LearnModel.h"

@interface XueXiController ()<GetHomeSideNewsDelegate>{
    NSInteger page;
}

@end

@implementation XueXiController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=_model.learn_tittle;
    self.Data_array = [NSMutableArray new];
    MJHiCarHeader *header           = [MJHiCarHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadnew)];
    header.automaticallyChangeAlpha    = YES;
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden           = YES;
    self.TableView.mj_header          = header;
    self.TableView.mj_header.automaticallyChangeAlpha = YES;
    [self.TableView.mj_header beginRefreshing];
    self.TableView.mj_footer           = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self loadmore];
    }];
    self.TableView.mj_footer.hidden    = YES;
    self.Data_array=[NSMutableArray new];
    [self.view addSubview:self.TableView];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}

- (void)loadmore{
    NewsModel *modellin = [self.Data_array lastObject];
    page = [modellin.news_id integerValue];
    
    NSMutableDictionary *postdic = [NSMutableDictionary new];
    [postdic setValue:[NSString stringWithFormat:@"%li",(long)page] forKey:@"page"];
    [postdic setValue:_model.learn_type forKey:@"type"];
    SideNewsViewModel *viewmodel = [[SideNewsViewModel alloc] init:postdic];
    viewmodel.delegate = self;
}

- (void)loadnew{
    page = 0;
    
    NSMutableDictionary *postdic = [NSMutableDictionary new];
    [postdic setValue:[NSString stringWithFormat:@"%li",(long)page] forKey:@"page"];
    [postdic setValue:_model.learn_type forKey:@"type"];
    SideNewsViewModel *viewmodel = [[SideNewsViewModel alloc] init:postdic];
    viewmodel.delegate = self;
}

- (void)GetHomeSideNewsSucces:(int)value array:(NSMutableArray *)array page:(NSInteger)page{
    if (value == 1) {
        if (page==0) {
            [self.Data_array removeAllObjects];
        }
        [self.Data_array addObjectsFromArray:array];
        
        if (array.count<15) {
            self.TableView.mj_footer.hidden = YES;
        }else{
            self.TableView.mj_footer.hidden = NO;
        }
    }
    [self.TableView.mj_header endRefreshing];
    [self.TableView.mj_footer endRefreshing];
    [self.TableView reloadData];
}


#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.Data_array.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}

#pragma mark - UITableView DataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HomeNewsCell *cell = [HomeNewsCell cellForTableView:tableView cellForRowAtIndexPath:indexPath];
    cell.Model = [self.Data_array objectAtIndex:indexPath.row];
    return cell;
}

#pragma mark - UITableView Delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NewsModel *modelnew = [self.Data_array objectAtIndex:indexPath.row];
    return modelnew.news_height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NewsModel *modelnew = [self.Data_array objectAtIndex:indexPath.row];
    [NewsAction Actiontodone:JonesGOTO_NewsInfo view:self dictionary:@{@"model":modelnew}];
    modelnew.IsRead = YES;
    [tableView reloadData];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
